import java.io.*;
import java.net.*;
import java.util.*;

public class HttpGet {

  private static int BUFFER_SIZE = 8096;
  private Vector vDownLoad = new Vector();
  private Vector vFileList = new Vector();

  public HttpGet() {

  }

  public void resetList() {
    vDownLoad.clear();
    vFileList.clear();
  }

  /**
   * add item 
   *
   * @param url String
   * @param filename String
   */
  public void addItem(String url, String filename) {
    vDownLoad.add(url);
    vFileList.add(filename);
  }

  /**
   * download by list
   */
  public boolean downLoadByList(DataOutputStream outputStream) {
    boolean bRet = true;
	  String url = null;
    String filename = null;
    
    //save file
    for (int i = 0; i < vDownLoad.size(); i++) {
      url = (String) vDownLoad.get(i);
      filename = (String) vFileList.get(i);

      try {
        bRet = saveToFile(url, filename,outputStream);
      }
      catch (IOException err) {
    	  System.out.println("source[" + url + "]download failure");
    	  bRet = false;
      }
    }
    return bRet;
  }

  /**
   * save to file
   *
   * @param destUrl String
   * @param fileName String
   * @throws Exception
   */
  public boolean saveToFile(String destUrl, String fileName,DataOutputStream outputStream) throws IOException {
    if(null == outputStream){
    	System.out.println("outputStream is null");
    	return false;
    }
	  boolean bRet = true;

	 FileOutputStream fos = null;
    BufferedInputStream bis = null;
    HttpURLConnection httpUrl = null;
    URL url = null;
    byte[] buf = new byte[BUFFER_SIZE];
    int size = 0;
    
    //create connect
    url = new URL(destUrl);
    httpUrl = (HttpURLConnection) url.openConnection();
   //connect url
    httpUrl.connect();
    
    //get file length
    long lFileLen=httpUrl.getContentLength();
     System.out.println("File length is:" + lFileLen);
    
    //get stream
    bis = new BufferedInputStream(httpUrl.getInputStream());
    //create file
    fos = new FileOutputStream(fileName);

	System.out.println("connecting [" + destUrl + "]\n save to [" + fileName + "]");

    //save file
	long passedlen = 0;
    while ( (size = bis.read(buf)) != -1) {
    	fos.write(buf, 0, size);
    	passedlen += size;
    	
    	try{
    		outputStream.writeUTF(String.valueOf(passedlen * 100 / lFileLen) + "%");
    		System.out.println("downloading...current percent:" + (passedlen * 100 / lFileLen)
    				+ "%\n");
	   }catch(Exception e){
	   	System.out.println("downloading failue\n");
		bRet = false;
		break;
	   }
    	
    }

    fos.close();
    bis.close();
    httpUrl.disconnect();
    return bRet;
}

  /**
   * set proxy
   *
   * @param proxy String
   * @param proxyPort String
   */
  public void setProxyServer(String proxy, String proxyPort) {

    System.getProperties().put("proxySet", "true");
    System.getProperties().put("proxyHost", proxy);
    System.getProperties().put("proxyPort", proxyPort);

  }
  
  
  /**
   * set user & pwd
   *
   * @param uid String
   * @param pwd String
   */
  /*public void setAuthenticator(String uid, String pwd) {
    Authenticator.setDefault(new MyAuthenticator(uid, pwd));
  }*/

  /**
   * main method 
   *
   * @param argv String[]
   */
  /*public static void main(String argv[]) {

    HttpGet oInstance = new HttpGet();
	try {
      	oInstance.addItem("http://www.tudou.com/programs/view/4X9r8Ar4dm4/","./c.html");
		//begin download
		oInstance.downLoadByList();
    }
    catch (Exception err) {
      System.out.println(err.getMessage());
    }

  }*/

}
